<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 商品分类
 * Class CategoryController
 * @package Home\Controller
 */

class CategoryController extends BaseController {

    public function index(){

        $local_id = 1;
        if ($_REQUEST['local_id']) {
            $local_id = intval($_REQUEST['local_id']);
        }

        $top_local_id = 11; // 如果是其他店铺，切换全国送的时候需要传一个其他门店的local_id方便切换
        if ($_REQUEST['top_local_id']) {
            $top_local_id = intval($_REQUEST['top_local_id']);
        }

        if($local_id != session('home_local_id')){
            session('home_local_id',null);
            session('home_local_id',$local_id);
        }
        if($top_local_id != session('top_home_local_id')){
            session('top_home_local_id',null);
            session('top_home_local_id',$top_local_id);
        }

        // 判断是游客没有默认门店就跳转
        if (empty($local_id)) {
            $local_id = session('t_userInfo')['default_local_id'];
            if(!$local_id){
                redirect(U('/First/stortSelect/'));
            }
        }

        // 查询全国送店铺
        $self_store = M('webpos_local_store')->cache(true, 86400, 'redis')->where(['is_self' => 1])->find();

        // 查询除开全国送的其他店铺
        $top_self_store = M('webpos_local_store')->cache(true, 86400, 'redis')->where(['local_id' => $top_local_id])->find();

        $catModel = M('b2c_store_cat');
        //查询店铺信息
        $storeData = M('webpos_local_store')->cache(true, 86400, 'redis')->where(['local_id'=>$local_id])->find();

        if (empty($storeData)) {
            redirect(U('/First/stortSelect/'));
        }

        $parent = $catModel->cache(true, 86400, 'redis')->where(['level' => 1, 'local_id' => $local_id])->select();

        foreach ($parent as $v ) {
            $second[] = $catModel->cache(true, 86400, 'redis')->where(['level' => 2, 'local_id' => $local_id, 'parent_id' => $v['store_cat_id']])->select();
        }

        $this->assign([
            'parent' => $parent,
            'second' => $second,
            'storeData' => $storeData,
            'self_store' => $self_store,
            'top_self_store' => $top_self_store,
        ]);

        $this->display();
    }




  

    

   

}